﻿using System;
using System.Linq;

namespace Fast.Extensions
{
    [SkipScan]
    public static class FastStringExtensions
    {
        /// <summary>
        /// 调用 <see cref="string.IsNullOrEmpty"/> 方法
        /// </summary>
        /// <param name="str"></param>
        /// <returns>如果参数为 <see cref="null"/> 或空字符串(<see cref="string.Empty"/> 或 "")，都返回true</returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        /// <summary>
        /// 调用 <see cref="string.IsNullOrWhiteSpace"/> 方法
        /// </summary>
        /// <param name="str"></param>
        /// <returns>如果参数为 <see cref="null"/> 或 空字符串(<see cref="string.Empty"/> 或 "") 或字符串完全由空白字符组成，都返回true</returns>
        public static bool IsNullOrWhiteSpace(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }

        /// <summary>
        /// 调用 <see cref="string.IsNullOrEmpty"/> 方法加上 ！
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNotNullOrEmpty(this string str)
        {
            return !str.IsNullOrEmpty();
        }

        /// <summary>
        /// 调用 <see cref="string.IsNotNullOrWhiteSpace"/> 方法加上 ！
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNotNullOrWhiteSpace(this string str)
        {
            return !str.IsNullOrWhiteSpace();
        }

        /// <summary>
        /// 移除字符串的末尾指定后缀
        /// </summary>
        /// <param name="str">目标字符串</param>
        /// <param name="postFixes">一个或多个后缀</param>
        /// <returns>修改后的字符串</returns>
        public static string RemovePostFix(this string str, params string[] postFixes)
        {
            // 依次比较
            return str.RemovePostFix(StringComparison.Ordinal, postFixes);
        }

        /// <summary>
        /// 移除字符串的末尾指定后缀
        /// </summary>
        /// <param name="str">目标字符串</param>
        /// <param name="comparisonType">字符串比较类型</param>
        /// <param name="postFixes">一个或多个后缀</param>
        /// <returns>修改后的字符串</returns>
        public static string RemovePostFix(this string str, StringComparison comparisonType, params string[] postFixes)
        {
            if (str.IsNullOrEmpty())
            {
                return str;
            }

            if (postFixes.IsNullOrEmpty())
            {
                return str;
            }

            foreach (var postFix in postFixes)
            {
                if (str.EndsWith(postFix, comparisonType))
                {
                    return str.Left(str.Length - postFix.Length);
                }
            }

            return str;
        }

        /// <summary>
        /// 从字符串的开头获取字符串的子字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static string Left(this string str, int len)
        {
            if (str.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(str));
            }

            if (str.Length < len)
            {
                throw new ArgumentException("len参数不能大于给定字符串的长度！");
            }

            return str.Substring(0, len);
        }

        /// <summary>
        /// 检查一个字段串是否在一个数组中存在
        /// </summary>
        /// <param name="str"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool IsExists(this string str, params string[] data)
        {
            foreach (var item in data)
            {
                if (str == item)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 驼峰命名
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        internal static string ToCamelCase(this string str)
        {
            if (string.IsNullOrWhiteSpace(str)) return str;

            return string.Concat(str.First().ToString().ToLower(), str.AsSpan(1));
        }
    }
}
